﻿<?php

	//接受更新请求参数
	$gameName = $_REQUEST['packageName'];//游戏名
	$gameVer = $_REQUEST['appVersion'];//版本号
	
	//数据库操作
	$conn = mysql_connect('localhost','root','') or die("connect db err:".mysql_error);
	mysql_select_db('info_push') or die("select db err:".mysql_error);
	mysql_query('set names utf8');
	
	//优先查询更新信息推荐
	$queryUpdate = "select  title,content from push where title like '/update/%' and gameName='{$gameName}' order by time desc limit 1";
	//echo $queryUpdate;
	$udp_res = mysql_query($queryUpdate,$conn) or die(mysql_error);
	$udp_info = mysql_fetch_assoc($udp_res);	
//	var_dump($queryUpdate);
	if(!empty($udp_info)){	
		if(is_int(strpos($udp_info['content'],$gameVer)))
		{
			echo "false0791";
		}
		else
		{
			list($title,$udp,$ver)=explode('/',$udp_info['title']);
			//$title无用途这里
			//只更新版本不推送消息
			if( cmpVersion($ver,$gameVer))//&& $title == $gameName )	//版本低于数据库版本	
			{
				echo $udp_info['content'];//打印更新地址
			}	
			else{//推送消息
				echo "false0791";
			}
		}		
	}else{
		echo "false0791";		
	}
	
	//比较版本
	function cmpVersion($udpVer/*数据库版本*/,$gameVer/*发送游戏版本*/)
	{
		list($dbPart1,$dbPart2,$dbPart3)=explode('.',$udpVer);
		list($part1,$part2,$part3)=explode('.',$gameVer);
		settype($dbPart1,'integer');settype($part1,'integer');
		if( ($dbPart1-$part1)>0 )	
			return true;			
		settype($dbPart2,'integer');settype($part2,'integer');
		if( ($dbPart2-$part2)>0 )	
			return true;			
		settype($dbPart3,'integer');settype($part3,'integer');
		if( ($dbPart3-$part3)>0 )		
			return true;		
		return false;
	}
?>